const db = wx.cloud.database()
Page({
  data: {
  }, 
  
  accessTokenFunc: function() {
    var that = this
    wx.cloud.callFunction({
      name: 'acctoken',
      success: res => {
        that.data.accessToken = res.result.access_token
        wx.setStorageSync("access_token", res.result.access_token)
        console.log(res)
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '调用失败',
        })
        console.error(err)
      }
    })
  },
  
  onLoad() {
    this.ctx = wx.createCameraContext()
    var accessToken = wx.getStorageSync("access_token")
    console.log("====accessToken===" + accessToken)
    if (accessToken == "" ||
        accessToken == null || accessToken == undefined) {
      this.accessTokenFunc()
    } else {
      this.setData({
        accessToken: wx.getStorageSync("access_token")
      })
    }
  },

  upload() {
    var that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sizeType:['compressed'],
      sourceType: ['album', 'camera'],
      camera: 'back',
      success: (res) => {
        src: res.tempFiles[0].tempFilePath
        console.log("上传成功")
        wx.showLoading({
          title: '正在加载中',
        }) 
        let size = res.tempFiles[0].size-5000000
        console.log(size)
        if (size) {
          wx.getFileSystemManager().readFile({
            filePath: res.tempFiles[0].tempFilePath,
            encoding: "base64",
            success: res => {
              console.log("图片读取成功！")
              that.req(that.data.accessToken, res.data)
            },
            fail: res => {
              wx.hideLoading()
              wx.showToast({
                title: '图片读取失败！',
                icon: "none"
              })
            }
          })
        }
        else{
          wx.showToast({
            icon:'none' ,   
            title:'识别图片大小最大为5M!'
          })
        }
      },
      fail: res => {
        wx.showToast({
          title: '上传失败！',
          icon: "none"
        })
      }
    })
  },

  req: function(token, image) {
    var that = this
    wx.cloud.callFunction({
      name: 'getan',
      data: {
        "image": image,
        "token": token
      },
      success: res => {
        wx.hideLoading()
        let data = res.result
        if ("result" in data) {
          var results = res.result.result
          console.log(results)
          if (results != undefined && results != null) {
            wx.navigateTo({ 
              url: '../info/info?name0=' + results[0].name + '&score0=' +  results[0].score + '&name1=' + results[1].name + '&score1=' +  results[1].score + '&name2=' + results[2].name + '&score2=' +  results[2].score
            })
          }
        }
        if ("error_code" in data) {
          wx.showToast({
            icon: 'none',
            title: 'AI识别失败！',
          })
          console.log(data)
        }
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '失败!可能是图片过大！',
        })
        console.log(err)
      }
    })
  },

  hideModal: function() {
    this.setData({
      isShow: false,
    })
  },
})